package net.zoneland.zrdp.biz.task;


import net.zoneland.zrdp.framework.web.service.WpsAttachmentService;
import net.zoneland.zrdp.framework.web.service.impl.WpsAttachmentServiceImpl;
import net.zoneland.zrdp.system.domain.SysConfig;
import net.zoneland.zrdp.system.service.ISysConfigService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 业务定时任务，业务定时任务的入口。
 */
@Component("bizTask")
public class BizTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(BizTask.class);

    @Resource
    private WpsAttachmentService attachmentService;

    @Resource
    private ISysConfigService configService;
    public void bizMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        LOGGER.info("biz执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i);
    }

    public void bizParams(String params)
    {
        LOGGER.info("biz执行有参方法：{}", params);
    }

    public void bizNoParams()
    {
        LOGGER.info("biz执行无参方法");
    }

    /**
     * wps appKey获取. 定时任务来更新wps appKey
     **/
    public void getNewAppKey() {
        LOGGER.info(">>>>>>>>>>>>开始运行【appKey获取】任务准备。。。。。。。");
        final String currentAppKey = attachmentService.getCurrentAppKey();
        if (StringUtils.isNotBlank(currentAppKey)) {
            Optional.ofNullable(attachmentService.getNewAppKey(currentAppKey)).filter(StringUtils::isNotBlank)
                .ifPresent(str -> {
                    final SysConfig sysConfig = configService.selectConfigByConfigKey(WpsAttachmentServiceImpl.WPS_APP_KEY);
                    sysConfig.setConfigValue(str);
                    configService.updateConfig(sysConfig);
                });
        }
        LOGGER.info(">>>>>>>>>>>>【appKey获取】任务运行结束。。。。。。。");
    }
}
